import ReactDOM from "react-dom";


// 表格分页查询
export function classPageQuery(page, queryObj) {
  let {curPage, pageSize, delCount} = queryObj;
  let query = {};

  if (curPage) {
    query.curPage = curPage;
    page.curPage = curPage;
  } else {
    query.curPage = page.curPage;
  }

  if (pageSize) {
    query.pageSize = pageSize;
    page.pageSize = pageSize;
  } else {
    query.pageSize = page.pageSize;
  }

  // 如果在最后一页全部删除，那么刷新时最页码就减一
  if (delCount && page.curPage > 1 && page.curPage === page.totalPage) {
    if (page.result.length === delCount) {
      query.curPage -= 1;
    }
  }

  return query;
}

/**
 * antd -v4 修改滚动条样式
 * 修改表格高度（内容滚动高度）
 * @param grid
 * @param tableHeight 有数据的高度 列子： 'calc(100vh - 510px)';
 * @param noDataHeight 无数据的高度 列子: 'calc(100vh - 510px - 96px)';
 * @private
 */
export function changeGridScrollStyle(grid, tableHeight, noDataHeight, overflowY, isMaxHeight) {
  overflowY = overflowY || 'scroll';
  try {
    let table_dom = ReactDOM.findDOMNode(grid);
    let body = table_dom.getElementsByClassName('ant-table-body')[0];
    let tbody = table_dom.getElementsByClassName('ant-table-tbody')[0];
    let normal = table_dom.getElementsByClassName('ant-empty-normal')[0];
    if (body) {
      if (isMaxHeight) {
        body.style.maxHeight = tableHeight;
      } else {
        body.style.height = tableHeight;
      }
      // body.style.overflowY = 'scroll';
      body.style.overflowY = overflowY;
      body.style.overflowX = 'auto';
    }
    if (tbody) {
      if (isMaxHeight) {
        body.style.maxHeight = tableHeight;
      } else {
        body.style.height = tableHeight;
      }
      tbody.style.overflowY = 'auto';
      tbody.style.overflowX = 'auto';
    }
    if (normal) {
      normal.style.height = noDataHeight;
    }
  } catch (e) {
    console.error(e)
  }

}
